<?php
/**
 * Created by PhpStorm.
 * User: maqun
 * Date: 2016/2/29
 * Time: 12:35
 * 竞赛&作业管理
 */

class ContestAction extends BaseAction{


    protected $page_size = 10;

    public function _initialize(){
        self::$action = 3;
        parent::_initialize();
    }

    /**
     * 竞赛&作业列表
     */
    public function index(){

        vendor('Page');

        $condition = I('get.condition');
        $where = !empty($condition) ? " locate(title,'".$condition."')" : '';
        $model = D('Contest');
        $count = $model->where($where)->count();

        $page  = new Page($count,$this->page_size);

        $list = $model->where($where)
            ->order('contest_id DESC')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
//        echo $model->_sql();
//        dump($list);exit;
        $this->assign('page',$page);
        $this->assign('list',$list);

        $this->display('index');
    }
    #添加竞赛&作业页面显示
    public function add(){

        $this->assign('now',date('Y-m-d H:i:s',time()));
        $this->assign('language',C('language'));
        $this->display('add');
    }
    #编辑显示页面
    public function edit(){

        $cid   = $this->_get('cid');
        $contestModel    = D('Contest');
        $conproblemModel = D('ContestProblem');
        $privilegeModel  = D('Privilege');

        $content = $contestModel->where("contest_id = $cid")->select();
        if(empty($content)){
            $this->error('数据不存在');
        }
        //获取已选择的语言
        $content[0]['lang']  = $this->langToArray($content[0]['langmask']);

        //获取问题id
        $plist               = $conproblemModel->field('problem_id')->where("contest_id = $cid")->order('num asc')->select();
        $content[0]['plist'] = $this->arrayToString($plist,'problem_id');

        //获取用户id
        $ulist               = $privilegeModel->field('user_id')->where("rightstr = 'c$cid'")->order('user_id asc')->select();
        $content[0]['ulist'] = $this->arrayToString($user_ids,'user_id');;

        $this->assign('language',C('language'));
        $this->assign('content',$content[0]);

        $this->display('edit');

    }
    #复制竞赛&作业
    public function copy(){

        $cid = $this->_get('cid');
        $contestModel = D('Contest');

        $res = $contestModel->copyContest($cid);

        if($res['status']){

            $this->assign('now',date('Y-m-d H:i:s',time()));
            $this->assign('language',C('language'));
            $this->assign('info',$res['info']);
            $this->display('add');
            exit;
        }else{

            $this->error($res['info']);
            exit;
        }
    }
    public function export(){
        die('export');
    }

    public function logs(){
        die('logs');
    }
    public function delete(){
        $get = $this->_get('pid');

        $News = D('Problem')->where("problem_id = $get")->delete();
        if($News){
            header("Location: /admin.php/Problem/index");
        }
    }
    #添加、修改提交保存数据
    public function save(){

        $post  = $this->_post();

        $model = D('Contest');

        $res   = $model->addContest($post);

        if($res['status']){
            $this->success($res['info'],U('/Contest/index'));
        }else{
            $this->error($res['info']);
        }
        exit;
    }

    #将户id或问题id组合成字符串
    public function arrayToString($result,$kword){

        $return = '';

        if(!empty($result)){
            foreach($result as $k=>$v){
                $return .= $k==(count($result)-1) ? $v[$kword]:$v[$kword].',';
            }
        }
        return $return;
    }
    #将选择的语言转换成数组
    public function langToArray($langmask){

        $lang_count = count(C('OJ_LAN_NAME'));
        $lang=(~((int)$langmask))&((1<<$lang_count)-1);

        $return = array();

        for($i=0;$i<$lang_count;$i++){
            $return[] = ($lang&(1<<$i))? 1 : 0 ;
        }
        return $return;
    }

}
